﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ShiQuan.Loggers
{
    /// <summary>
    /// 日志存储
    /// </summary>
    public class Logger
    {
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="msg"></param>
        public static void Info(string msg)
        {
            Logger.Info(string.Empty, msg);
        }
        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="classify"></param>
        /// <param name="msg"></param>
        public static void Info(string classify,string msg)
        {
            StringBuilder fileContent = new StringBuilder();
            DateTime now = DateTime.Now;
            try
            {
                fileContent.AppendLine("====");
                fileContent.AppendLine("now=" + now.ToString("yyyy-MM-dd HH:mm:ss"));
                fileContent.AppendLine("msg=" + msg);

                string mSavePath = GetSavePath();
                string mSaveFile = System.IO.Path.Combine(mSavePath, now.ToString("yyyyMMddHH")+"_"+classify+".log");
                System.IO.File.AppendAllText(mSaveFile, fileContent.ToString(), Encoding.UTF8);
            }
            catch (Exception ex)
            {
                fileContent.AppendLine("error=" + ex.ToString());
                string mSavePath = GetSavePath(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Log");
                string mSaveFile = System.IO.Path.Combine(mSavePath, now.ToString("yyyyMMddHH") + "_" + classify + ".log");
                System.IO.File.AppendAllText(mSaveFile, fileContent.ToString(), Encoding.UTF8);
            }
        }
        /// <summary>
        /// 异常
        /// </summary>
        /// <param name="msg"></param>
        public static void Error(string msg)
        {
            StringBuilder fileContent = new StringBuilder();
            DateTime now = DateTime.Now;
            try
            {
                fileContent.AppendLine("====");
                fileContent.AppendLine("now=" + now.ToString("yyyy-MM-dd HH:mm:ss"));
                fileContent.AppendLine("msg=" + msg);

                string mSavePath = GetSavePath();
                string mSaveFile = System.IO.Path.Combine(mSavePath, now.ToString("yyyyMMddHH") + "_error.log");
                System.IO.File.AppendAllText(mSaveFile, fileContent.ToString(), Encoding.UTF8);
            }
            catch (Exception ex)
            {
                fileContent.AppendLine("error=" + ex.ToString());
                string mSavePath = GetSavePath(System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\Log");
                string mSaveFile = System.IO.Path.Combine(mSavePath, now.ToString("yyyyMMddHH") + "_error.log");
                System.IO.File.AppendAllText(mSaveFile, fileContent.ToString(), Encoding.UTF8);
            }
        }

        private static string GetSavePath()
        {
            string mSavePath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Log");
            return GetSavePath(mSavePath);
        }

        private static string GetSavePath(string savepath)
        {
            string mSavePath = System.IO.Path.Combine(savepath, DateTime.Today.ToString("yyyyMMdd"));
            if (System.IO.Directory.Exists(mSavePath) == false)
                System.IO.Directory.CreateDirectory(mSavePath);

            //获取保存目录下所有子目录
            string[] mSubPaths = System.IO.Directory.GetDirectories(savepath);
            for (int i = 0; i < mSubPaths.Length; i++)
            {
                if(mSavePath != mSubPaths[i])
                {
                    try
                    {
                        System.IO.Directory.Delete(mSubPaths[i], true);
                    }
                    catch{ }
                }
            }

            return mSavePath;
        }
    }
}
